
template.config("openTag", '{');
template.config("closeTag", '}');	
template.config("compress", true);	
$('table').footable({ breakpoints: {tiny: 460, medium: 650, big: 960}});
		
var beanlist = {
		
	url_useradd : encodeURL('/resources/views/jspmode/beanadd.html'),
	url_usermodify : encodeURL('/jspmode/beanmodify?userId='),
	html_loading : '<tr><td colspan="7" class="text-center"><i class="fa fa-spin fa-spinner"></i></td></tr>',
	
	fInit: function() {
		this.fLoadGrid();
    	this.fBindEvent();         
    },

	fBindEvent : function(){	
		/*全选/反选*/
		$("thead .i-checks i").click(function(){
			$("tbody .i-checks i").trigger("click");    
	    }); 
		
		/*按组织查询用户*/
	    $("a[js-org-label]").click(function(){
	    	if($(this).hasClass("hover")){
	    		$("#currentorgname").removeData("id").text('All of company');
	    		$(this).removeClass("hover");
	    		beanlist.fLoadGrid();
	    	}else {
	    		$("#currentorgname").data("id",$(this).attr("id")).text($(this).children("span").text());
		    	$(this).parent().children("a").removeClass("hover");
		    	$(this).addClass("hover");
		    	beanlist.fLoadGrid($(this).attr("id"));
	    	}
	    });

	    /*新增用户*/
	    $("#add").click(function(){
	    	$('.modal-content').load(beanlist.url_useradd);
	    	$('#dialog').modal({backdrop:'static'});
	    	$('#dialog').on('shown.bs.modal', function () { //窗口渲染完成后		    		
	    		var selectObj = $("select[name='userOrgId']");
	    		selectObj.html(''); //清空组织列表		    		
	    		$("a[js-org-label]").each(function(){ //重新设置组织选项的列表数据
	    			selectObj.append("<option value='"+$(this).attr("id")+"'>"+$(this).children("span").text()+"</option>");
	    		});
	    		var orgid = $("#currentorgname").data("id");
	    		if(orgid != undefined && orgid != null){
	    			selectObj.val($("#currentorgname").data("id")); //默认匹配当前所查询的组织
	    		}
	    	});		    	
	    });
	    
	    /*编辑用户*/
	    $("#modify").click(function(){
	    	var choises = $('tbody input:visible:checked');
	    	if(choises.size() != 1){
	    		$.toaster({ priority : 'warning', message : 'only one item should be checked.'});
	    	}else {
	    		$('.modal-content').load(beanlist.url_usermodify+choises.val());
	    		$('#dialog').modal({backdrop:'static'});
	    	}
	    });
	    
	    /*删除用户*/
	    $("#delete").click(function(){
	    	var choises = $('tbody input:visible:checked');
	    	if(choises.size() < 1){
	    		$.toaster({ priority : 'warning', message : 'at least one item should be checked.'});
	    	}else {
	    		var isdel = window.confirm("do you realy want to delete these items?");
	    		if(isdel){
	    			beanlist.fUserDel();
	    		}
	    	}
	    });
	},
	
	/*type: add新增;modify编辑*/
	fUserOperation : function(type){
		$('#userform span').text(''); //清空验证错误提示
		$.ajax({
			url : encodeURL('/jspmode/bean'+type),
			data : $('#userform').serializeArray(),
			dataType : 'json',
        	type : 'POST',                                              
            success : function(result) {
                if(result.success) {
                	beanlist.fLoadGrid(); //刷新数据
                	$.toaster({ priority : 'success', message : 'operation complete success!'}); //成功提示
                	$('#dialog').modal('hide');	//隐藏窗口
                	$('#userform')[0].reset(); //重置表单
                }else if(result.datas.validate != undefined){ //有验证错误
                	$.each(result.datas.validate.allFieldErrorMessage, function(i, n){ 
                		$("#userform [name='"+i+"']").next().text(n[0]); //显示验证错误
                	});               	
                }else { //系统异常
                	$.toaster({ priority : 'danger', message : result.i18nMessage});
                }
            }
        });						
	},	
	
	fUserDel : function(){
		var choises = $('tbody input:visible:checked'); 
		$.ajax({
			url : encodeURL('/jspmode/beandel'),
			data : choises.serializeArray(),
			dataType : 'json',
        	type : 'POST',                                              
            success : function(result) {
                if(result.success) {
                	$('table').data('footable').removeRow(choises.closest('tr'));//刷新数据
                	$.toaster({ priority : 'success', message : 'operation complete success!'}); //成功提示
                }else { //系统异常
                	$.toaster({ priority : 'danger', message : result.i18nMessage});
                }
            }
        });						
	},
	
	fLoadGrid : function(orgId){
		$('table tbody').html(beanlist.html_loading);
		$('table tfoot').hide(); //强制隐藏分页栏
		
		var data = {};
		if(orgId != null && orgId != undefined){
			data = {userOrgId:orgId};
		}								
		$.get(encodeURL('/jspmode/userlist'), data,
			function(result){
				if(result.success){
					$('table tfoot').removeAttr("style"); //取消强制隐藏，交由foo自己判断
					$('table tbody').html(template('tpl', result.datas)); //设置数据					
					$('table tbody').trigger('footable_redraw'); //重绘表格
				}else {
					$.toaster({ priority : 'danger', message : result.i18nMessage});
					$('table tbody').html('');
				}
			}
		);	
	}
};

beanlist.fInit();	
	

